from apps.gzde.models import *
from extensions.serializers import *


class GoodsSerializer(BaseSerializer):
    creator_name = CharField(source='creator.name', read_only=True, label='创建者')
    updater_name = CharField(source='updater.name', read_only=True, label='更新者')

    class ImageSerializer(BaseSerializer):
        src = CharField(read_only=True, label='资源地址')
        alt = CharField(read_only=True, label='提示')
        class Meta:
            model = Attachment
            fields = ['id', 'src', 'alt', 'name', 'type', 'file']

        # 将返回的数据进行处理
        def to_representation(self, instance):
            data = super().to_representation(instance)
            data['src'] = data.pop('file')
            data['alt'] = data['name']
            return data

    images_items = ImageSerializer(source='images', many=True, read_only=True, label='商品图片')

    class Meta:
        model = Goods
        fields = '__all__'


    def create(self, validated_data):
        validated_data['creator'] = self.context['request'].user
        validated_data['updater'] = self.context['request'].user
        instance = Goods(**validated_data)
        instance.save()
        return instance

    def update(self, instance, validated_data):
        validated_data['updater'] = self.context['request'].user
        return super().update(instance, validated_data)


class GoodsCategorySerializer(BaseSerializer):
    creator_name = CharField(source='creator.name', read_only=True, label='创建者')
    updater_name = CharField(source='updater.name', read_only=True, label='更新者')
    children = SerializerMethodField(read_only=True, label='子类')

    class Meta:
        model = GoodsCategory
        fields = '__all__'

    def get_children(self, obj):
        """获取子类"""
        if obj.children.all().exists():
            return GoodsCategorySerializer(obj.children.order_by('sort'), many=True).data
        return None

    def create(self, validated_data):
        validated_data['creator'] = self.context['request'].user
        validated_data['updater'] = self.context['request'].user
        instance = GoodsCategory(**validated_data)
        instance.save()
        return instance

    def update(self, instance, validated_data):
        validated_data['updater'] = self.context['request'].user
        return super().update(instance, validated_data)



class AttachmentSerializer(BaseSerializer):
    creator_name = CharField(source='creator.name', read_only=True, label='创建者')
    class Meta:
        model = Attachment
        read_only_fields = ['id', 'name', 'type', 'creator_name', 'create_time', 'updater', 'update_time']
        fields = ['file','is_index', *read_only_fields]

    def create(self, validated_data):
        validated_data['name'] = validated_data['file'].name
        validated_data['type'] = self.request.data['type']
        validated_data['creator'] = self.user
        validated_data['updater'] = self.user
        return super().create(validated_data)


class IndexBannerSerializer(BaseSerializer):
    creator_name = CharField(source='creator.name', read_only=True, label='创建者')
    src = SerializerMethodField()
    alt=CharField(source='image.name', read_only=True, label='提示')
    class Meta:
        model = IndexBanner
        fields = '__all__'

    def create(self, validated_data):
        validated_data['creator'] = self.user
        validated_data['updater'] = self.user
        return super().create(validated_data)

    def get_src(self, obj):
        request = self.context.get('request')
        return request.build_absolute_uri(obj.image.file.url)